<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';



class hd_survey_customer extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Hdsurveymodel','m_survey');
        $this->load->model('Usermodel','m_user');
    }

    public function index(){
        $data = parent::index();
        $data['includeExcel'] = 1;
        $province = $this->m_user->getUserProvinceList4js();
        $data['province'] = json_encode($province);
        $this->load->view('admin/hd_survey_customer',$data);
    }

    public function getCustomerList(){
        $assist['where'] = getFilter($this->input->post(),Hdsurveymodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Hdsurveymodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Hdsurveymodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        $assist['select'] = 'hd_survey_customer.*,user.user_name,hd_survey_content.score,';
        $arr = array(3,3,4,3,3);
        foreach ($arr as $i => $len) {
            for($j = 0; $j<$len; $j++) {
                $assist['select'] .= 'hd_survey_content.question_'.($i+1).'_'.($j+1).',';
            }
        }
        $assist['select'] .= 'user.user_id,user.user_code,user.user_province,user.user_city';

        $user = $this->session->userdata('user');
        // 4s店
        if($user['group_id'] == 2) {
            $assist['where']['hd_survey_customer.user_id'] = $this->m_app->getCurrentUserId();
        }

        if(isset($assist['where']['hd_survey_customer.content_id'])) {
            $content_id = $assist['where']['hd_survey_customer.content_id'];
            unset($assist['where']['hd_survey_customer.gift_money']);
            $assist['where'][$content_id > 0 ? 'hd_survey_customer.content_id >' : 'hd_survey_customer.content_id'] ='0';
        }
        list($list,$count) = $this->m_survey->getCustomerListSection($assist);
        //
        // foreach ($list as $key => $value) {
        //     $list[$key]['nickname'] = urldecode($value['nickname']);
        // }

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function exportShopScore() {
        @set_time_limit(0);
        @ini_set('memory_limit', '3000M');

        $sql = '';
        $sql .= 'mpc_hd_survey_content.score,customer_time_cost,mpc_user.user_name,mpc_user.user_id,mpc_user.user_code,mpc_user.user_province,mpc_user.user_city';
        $sql = 'select '.$sql.' from mpc_hd_survey_content left join mpc_user
                ON mpc_hd_survey_content.user_id = mpc_user.user_id where question_1_1>0';

        $query = $this->db->query($sql);
        $list = $query->result_array();

        $data = array();

        foreach ($list as $k => $v) {
            if(!isset($data[$v['user_code']])) $data[$v['user_code']] = array();
            if($v['customer_time_cost'] < 10) continue;
            $data[$v['user_code']][] = $v;
            unset($list[$k]);
        }

        $list = array();
        function cmp($a, $b)
        {
            return ($a['score'] >= $b['score']) ? -1 : 1;
        }
        $shop_data = json_decode('{"DAH001":165,"DAH002":91,"DAH003":126,"DAH004":86,"DAH005":155,"DAH006":84,"DAH007":87,"DAH008":149,"DAH009":93,"DAH010":76,"DAH011":79,"DAH012":46,"DAH013":83,"DAH014":73,"DAH015":67,"DAH016":31,"DAH017":60,"DBJ001":150,"DBJ002":202,"DBJ003":158,"DBJ004":176,"DBJ005":165,"DBJ006":156,"DBJ007":111,"DBJ008":201,"DBJ009":215,"DBJ011":93,"DBJ012":119,"DBJ013":68,"DBJ014":287,"DBJ015":181,"DBJ016":147,"DBJ018":171,"DBJ019":94,"DBJ020":70,"DCQ001":232,"DCQ002":202,"DCQ003":185,"DCQ004":247,"DCQ005":32,"DCQ006":10,"DFJ003":163,"DFJ004":109,"DFJ005":108,"DFJ006":160,"DFJ007":175,"DFJ008":143,"DFJ009":285,"DFJ010":106,"DFJ012":91,"DFJ014":119,"DFJ016":142,"DFJ017":146,"DFJ018":190,"DFJ020":79,"DFJ022":32,"DFJ023":57,"DFJ024":8,"DGD001":373,"DGD002":252,"DGD003":241,"DGD004":306,"DGD005":323,"DGD006":369,"DGD007":0,"DGD008":261,"DGD009":226,"DGD010":218,"DGD011":318,"DGD012":282,"DGD013":223,"DGD014":99,"DGD015":186,"DGD016":126,"DGD017":324,"DGD018":306,"DGD019":157,"DGD020":168,"DGD021":19,"DGD022":307,"DGD023":269,"DGD024":276,"DGD025":228,"DGD026":228,"DGD027":255,"DGD028":131,"DGD029":193,"DGD030":288,"DGD031":281,"DGD032":328,"DGD033":197,"DGD034":260,"DGD035":285,"DGD036":151,"DGD037":248,"DGD038":165,"DGD039":177,"DGD040":225,"DGD041":237,"DGD042":162,"DGD043":249,"DGD044":141,"DGD045":244,"DGD046":367,"DGD047":130,"DGD049":230,"DGD050":246,"DGD051":166,"DGD052":291,"DGD053":221,"DGD054":175,"DGD055":204,"DGD056":171,"DGD057":257,"DGD058":156,"DGD059":122,"DGD060":136,"DGD061":208,"DGD062":238,"DGD063":206,"DGD064":207,"DGD065":130,"DGD066":63,"DGD068":174,"DGD069":161,"DGD071":179,"DGD072":174,"DGD073":130,"DGD075":141,"DGD076":100,"DGD077":77,"DGD078":134,"DGD079":212,"DGD080":17,"DGS001":191,"DGS002":142,"DGS003":27,"DGX001":130,"DGX002":158,"DGX003":79,"DGX004":150,"DGX005":125,"DGX006":121,"DGX007":90,"DGX008":123,"DGX009":101,"DGX010":72,"DGX011":47,"DGZ002":206,"DGZ003":82,"DGZ004":78,"DGZ005":71,"DGZ006":40,"DGZ007":128,"DHAN01":209,"DHAN02":206,"DHAN03":107,"DHEB01":320,"DHEB02":110,"DHEB03":167,"DHEB04":163,"DHEB05":121,"DHEB06":134,"DHEB07":102,"DHEB08":145,"DHEB09":117,"DHEB10":152,"DHEB11":166,"DHEB12":48,"DHEB13":170,"DHEB14":112,"DHEB15":142,"DHEB16":29,"DHEB18":25,"DHEB19":93,"DHEB20":13,"DHEB21":13,"DHEN01":109,"DHEN02":126,"DHEN03":241,"DHEN04":307,"DHEN05":202,"DHEN06":37,"DHEN08":76,"DHEN10":103,"DHEN12":116,"DHEN13":84,"DHEN14":121,"DHEN15":99,"DHEN16":58,"DHEN17":36,"DHEN19":161,"DHEN20":58,"DHEN21":16,"DHEN22":81,"DHEN23":73,"DHEN25":21,"DHEN26":63,"DHEN27":27,"DHEN28":23,"DHEN29":106,"DHEN30":66,"DHEN31":3,"DHLJ01":21,"DHLJ02":221,"DHLJ03":73,"DHLJ04":78,"DHLJ05":62,"DHLJ06":323,"DHLJ07":79,"DHLJ08":59,"DHLJ13":82,"DHLJ14":51,"DHUB01":437,"DHUB02":525,"DHUB03":91,"DHUB04":245,"DHUB05":96,"DHUB06":134,"DHUB07":168,"DHUB08":63,"DHUB09":218,"DHUB10":61,"DHUB11":58,"DHUB12":223,"DHUB13":69,"DHUB14":137,"DHUB15":56,"DHUB16":44,"DHUB17":120,"DHUN01":246,"DHUN02":410,"DHUN03":169,"DHUN05":270,"DHUN06":141,"DHUN07":121,"DHUN09":129,"DHUN10":189,"DHUN11":162,"DHUN12":88,"DHUN13":94,"DHUN14":72,"DHUN15":128,"DHUN16":97,"DHUN17":94,"DHUN18":37,"DHUN20":24,"DHUN21":90,"DHUN22":79,"DJL001":196,"DJL002":141,"DJL003":177,"DJL004":65,"DJL005":63,"DJL010":136,"DJL011":3,"DJL012":57,"DJL013":35,"DJS001":284,"DJS002":95,"DJS003":334,"DJS004":71,"DJS005":60,"DJS006":151,"DJS007":129,"DJS008":128,"DJS009":175,"DJS010":160,"DJS011":93,"DJS012":175,"DJS013":92,"DJS014":140,"DJS015":100,"DJS016":243,"DJS017":154,"DJS018":155,"DJS020":97,"DJS021":143,"DJS022":140,"DJS024":153,"DJS025":109,"DJS026":97,"DJS027":76,"DJS028":117,"DJS029":124,"DJS030":28,"DJS031":191,"DJS033":57,"DJS034":138,"DJS035":136,"DJS036":142,"DJS037":68,"DJS038":129,"DJS040":5,"DJS041":54,"DJS042":81,"DJS043":26,"DJS044":5,"DJS045":51,"DJX001":119,"DJX002":107,"DJX005":112,"DJX006":77,"DJX007":83,"DJX008":124,"DJX009":91,"DJX010":80,"DJX011":85,"DJX012":123,"DJX015":25,"DLN001":227,"DLN002":357,"DLN004":163,"DLN005":71,"DLN006":292,"DLN007":260,"DLN008":80,"DLN009":120,"DLN010":178,"DLN011":93,"DLN012":175,"DLN013":59,"DLN014":78,"DLN015":99,"DLN016":77,"DLN017":54,"DLN019":118,"DLN020":65,"DLN022":5,"DLN023":9,"DLN024":7,"DNMG02":78,"DNMG03":48,"DNMG04":138,"DNMG05":100,"DNMG08":67,"DNMG09":27,"DNMG10":20,"DNMG11":27,"DNMG12":16,"DNX001":87,"DNX002":54,"DQH001":101,"DSC001":202,"DSC002":196,"DSC003":114,"DSC005":143,"DSC006":113,"DSC008":109,"DSC009":27,"DSC010":70,"DSC011":263,"DSC014":73,"DSC015":88,"DSC016":43,"DSC019":42,"DSC020":8,"DSC021":43,"DSC022":38,"DSC023":26,"DSC024":18,"DSD002":80,"DSD003":58,"DSD004":79,"DSD005":116,"DSD006":133,"DSD007":73,"DSD008":157,"DSD010":112,"DSD011":99,"DSD013":48,"DSD014":59,"DSD015":101,"DSD018":78,"DSD019":129,"DSD020":81,"DSD021":181,"DSD023":84,"DSD024":40,"DSD025":100,"DSD026":69,"DSD028":83,"DSD031":16,"DSD033":136,"DSD034":68,"DSD036":68,"DSD037":71,"DSD038":70,"DSD039":95,"DSD041":44,"DSD042":25,"DSD043":90,"DSD044":36,"DSD045":19,"DSD046":31,"DSD047":87,"DSD048":90,"DSD049":2,"DSH001":316,"DSH003":232,"DSH004":123,"DSH005":142,"DSH008":165,"DSH011":190,"DSH012":72,"DSH013":147,"DSH014":151,"DSXE02":155,"DSXE03":140,"DSXE04":67,"DSXE05":138,"DSXE06":113,"DSXE07":119,"DSXE08":1,"DSXE09":42,"DSXE10":33,"DSXE11":104,"DSXE12":110,"DSXE13":36,"DSXE14":58,"DSXE16":202,"DSXW01":184,"DSXW02":27,"DSXW03":58,"DSXW04":198,"DSXW06":114,"DSXW07":7,"DSXW08":16,"DSXW09":39,"DSXW12":45,"DTJ001":204,"DTJ002":36,"DTJ004":189,"DTJ005":123,"DTJ006":154,"DTJ007":76,"DXJ001":154,"DXJ002":124,"DXJ003":29,"DXJ004":60,"DXJ005":28,"DXZ001":1,"DYN001":184,"DYN003":99,"DYN004":117,"DYN006":76,"DYN007":39,"DYN010":32,"DYN011":23,"DYN012":42,"DYN013":22,"DZJ001":113,"DZJ002":158,"DZJ003":88,"DZJ005":216,"DZJ006":47,"DZJ007":102,"DZJ009":150,"DZJ011":132,"DZJ012":78,"DZJ013":120,"DZJ014":126,"DZJ015":89,"DZJ017":110,"DZJ018":224,"DZJ020":78,"DZJ021":152,"DZJ023":82,"DZJ024":74,"DZJ025":168,"DZJ026":138,"DZJ027":122,"DZJ028":54,"DZJ029":172,"DZJ030":111,"DZJ031":117,"DZJ032":118,"DZJ033":45,"DZJ034":92,"DZJ035":102,"DZJ036":76,"DZJ038":205,"DZJ040":96,"DZJ041":93,"DZJ042":66,"DZJ043":51,"DZJ045":99,"DZJ046":71,"DZJ048":69,"DZJ049":38,"DZJ050":187,"DZJ051":84,"DZJ052":55,"DZJ053":28}',true);

        foreach ($shop_data as $k => $limit) {
            if(!isset($data[$k])) {
                // echo $k.'不存在数据中';
                // die();
                $list[] = array('user_code' => $k, 'limit' => $limit, 'user_name' => '', 'score' => '', 'count' => '');
                continue;
            }
            $v = $data[$k];

            usort($v,"cmp");
            $score = 0;
            for($i = 0; $i < min(count($v),$limit); $i++) {
                $score += $v[$i]['score'];
            }
            $v[0]['score'] = intval($score/$limit*100)/100;
            $v[0]['limit'] = $limit;
            $v[0]['count'] = count($v);
            $list[] = $v[0];
        }

        echo json_encode($list);

        // echo json_encode($list);

        $this->load->library('zmexcel');
        $this->zmexcel->init();
        $this->zmexcel->setTitle(array(
            array('content'=>'店铺名称'),
            array('content'=>'店铺编码'),
            array('content'=>'最低问卷数'),
            array('content'=>'参与人数'),
            array('content'=>'平均打分'),
        ));

        foreach ($list as $key => $value) {
            $this->zmexcel->addRow(array(
                array('content' => $value['user_name']),
                array('content' => $value['user_code']),
                array('content' => $value['limit']),
                array('content' => $value['count']),
                array('content' => $value['score']),
            ));
            unset($list[$key]);
        }


        // $this->excel->freezePanes(1,0);
        // $this->zmexcel->generate('广汽售后调查平均打分情况_'.date('YmdHis').'.xls');
    }

}
/* End of file customer.php */
/* Location: ./application/controllers/customer.php */
